import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, FormControl } from '@angular/forms';
import { map } from 'rxjs/operators';

@Component({
	selector: 'm-iptable-edit',
	templateUrl: './iptable-edit.component.html',
	styleUrls: ['./iptable-edit.component.scss'],
	providers: [
		{
			provide: NG_VALUE_ACCESSOR,
			useExisting: IptableEditComponent,
			multi: true,
		},
	],
})
export class IptableEditComponent implements OnInit, ControlValueAccessor {
	constructor() {}
	@Input() readonly = false;
	control = new FormControl('');
	line$ = this.control.valueChanges.pipe(map((val: string) => val.split('\n').length));
	ngOnInit() {}

	writeValue(obj: string[] = []): void {
		if (obj instanceof Array) {
			this.control.setValue(obj.join('\n'));
		}
	}
	registerOnChange(fn: any): void {
		this.control.valueChanges.pipe(map((value: string) => value.split('\n'))).subscribe(fn);
	}
	registerOnTouched(fn: any): void {}
	setDisabledState?(isDisabled: boolean): void {
		this.control.registerOnDisabledChange;
		if (isDisabled) {
			this.control.disable();
		} else {
			this.control.enable();
		}
	}
}
